<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>

        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import?type=employees')">excel导入</el-button>
          <el-button type="danger" size="small" @click="exportExcel">excel导出</el-button>
          <el-button type="primary" size="small" @click="clickAddEmployee('point_user_add')">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table v-loading="isLoading" :data="tableData" border>
          <el-table-column label="序号" sortable="" type="index" width="60" :index="indexMethod" />
          <el-table-column label="姓名" sortable="" prop="username" />

          <el-table-column label="头像" sortable="" prop="staffPhoto">
            <template #default="{row}">
              <img v-imgError="imgError" class="staff" :src="row.staffPhoto || defaultImg" alt="" @click="clickImg(row.staffPhoto)">
            </template>
          </el-table-column>

          <el-table-column label="手机号" sortable="" prop="mobile" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column label="聘用形式" sortable="">
            <template #default="{row }">
              {{ +row.formOfEmployment===1?'正式':'非正式' }}
            </template>
          </el-table-column>
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="" prop="timeOfEntry">
            <template #default="{row }">
              {{ row.timeOfEntry | formatTime }}
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row }">
              <el-button type="text" :disabled="!checkLook('point_user_look')" size="small" @click="$router.push(`/details/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="assignRole(row.id)">角色</el-button>
              <el-button type="text" :disabled="!checkDel('point_user_del')" size="small" @click="delEmployee( row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 二维码弹出层 -->
        <el-dialog
          title="提示"
          :visible.sync="quickResponseVisible"
          width="30%"
        >
          <div style="text-align:center">
            <canvas ref="Mycanvas" />
          </div>
          <template #footer>
            <el-button @click="quickResponseVisible = false">取 消</el-button>
            <el-button type="primary" @click="quickResponseVisible = false">确 定</el-button>
          </template>
        </el-dialog>

        <!-- 角色分配弹出层 -->
        <AssignRole :show-role-dialog.sync="showRoleDialog" :user-id="userId" />

        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <!--
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
             -->
          <el-pagination
            :current-page="page"
            :page-sizes="[1, 2, 3, 4]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </div>
      </el-card>
      <AddEmployee :show-dialog.sync="showDialog" @getEmployeeList="getEmployeeList" />

    </div>
  </div>
</template>

<script>
import qrcode from 'qrcode'
import constantObj from '@/constant/employees'
import AddEmployee from '@/views/employees/components/add-employee.vue'
import { reqDelEmployee, reqGetEmployeeList } from '@/api/employees'
import { exportFormatData } from '@/utils'
import AssignRole from '@/views/employees/components/assign-role.vue'
import checkLook from '@/mixin'
import checkDel from '@/mixin/checkDel'
import clickAddEmployee from '@/mixin/clickAddEmployee'
export default {
  name: 'Employees',
  components: {
    AddEmployee,
    AssignRole
  },
  mixins: [checkLook, checkDel, clickAddEmployee],
  data() {
    return {
      tableData: [],
      page: 1,
      size: 2,
      total: 0,
      hireType: constantObj.hireType,
      isLoading: false,
      showDialog: false,
      defaultImg: 'https://img1.baidu.com/it/u=1545655807,459773094&fm=26&fmt=auto&gp=0.jpg',
      imgError: require('@/assets/common/head.jpg'),
      quickResponseVisible: false,
      showRoleDialog: false,
      userId: ''

    }
  },
  created() {
    this.getEmployeeList()
    console.log('roles', this.$store.getters.roles)
  },
  methods: {
    async getEmployeeList() {
      this.isLoading = true
      const { rows, total } = await reqGetEmployeeList(this.page, this.size)
      this.tableData = rows
      this.total = total
      this.isLoading = false
    },
    handleCurrentChange(val) {
      this.page = val
      this.getEmployeeList()
    },
    handleSizeChange(val) {
      this.size = val
      this.page = 1
      this.getEmployeeList()
    },
    indexMethod(index) {
      return (this.page - 1) * this.size + index + 1
    },
    async delEmployee(id) {
      await reqDelEmployee(id)
      this.getEmployeeList()
    },

    exportExcel() {
      import('@/vendor/Export2Excel').then(async excel => {
        const headersArr = ['姓名', '手机号', '入职日期', '聘用形式', '转正日期', '工号', '部门']

        const { rows } = await reqGetEmployeeList(1, this.total)

        excel.export_json_to_excel({
          header: headersArr,
          data: exportFormatData(rows),
          filename: 'excel-list',
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    async clickImg(url) {
      if (!url) return this.$message.success('用户没有上传图片')

      this.quickResponseVisible = true
      await this.$nextTick()
      qrcode.toCanvas(this.$refs.Mycanvas, url)
    },
    assignRole(id) {
      this.showRoleDialog = true
      this.userId = id
    }

  }

}
</script>

<style lang="scss" scoped>
.employees-container {
  .staff {
    width: 70px;
    height: 70px;
    border-radius: 50%;
  }
}
</style>
